package com.sdx.console.base.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.service.ResCloudFileService;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.base.entity.BaseUniv;
import com.sdx.console.base.service.BaseUnivService;
import com.sdx.console.common.service.BaseDataService;
import com.sdx.console.system.service.DictBusinessService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 院校 接口
 */
@RequestMapping("/base/baseUniv")
@Controller
public class BaseUnivController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(BaseUnivController.class);
    private static final String MODEL = "/base/baseUniv";


    @Resource
	private BaseUnivService baseUnivService;
    
    @Resource
	ResCloudFileService resCloudFileService;

	@Resource
	private BaseDataService baseDataService;

	@Resource
	private DictBusinessService dictBusinessService;

    /* 页面 */

    @GetMapping("/index.do")
    @Function("base.baseUniv.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        return view;
    }

    @GetMapping("/edit.do")
    @Function("base.baseUniv.edit")
    @ResponseBody
    public ModelAndView edit(Integer univId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        //queryById(univId);
        BaseUniv baseUniv = baseUnivService.selectBaseUnivById(univId);
        view.addObject("baseUniv", baseUniv);
		view.addObject("univTypeList",dictBusinessService.getUnivTypeList());
		try {
			view.addObject("baseUnivJson", JacksonUtils.toJsonString(baseUniv,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("base.baseUniv.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("base.baseUniv.query")
    @ResponseBody
    public JsonResult<PageQuery> list(BaseUniv condtion)
    {
        PageQuery page = condtion.getPageQuery();
        baseUnivService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,BaseUniv baseUniv,HttpServletRequest request
							,MultipartFile logoFile
							,MultipartFile backGroundFile
	) {
    	JsonResult result = JsonResult.success();
		CoreUser user = getLoginUser(request);
    	if (isNew) {
			baseUnivService.save(baseUniv);
		} else {
			baseUnivService.updateTemplate(baseUniv);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("base.baseUniv.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)BaseUniv baseUniv,HttpServletRequest request
							,@RequestParam("logoFile") MultipartFile logoFile
							,@RequestParam("backGroundFile") MultipartFile backGroundFile
		)
    {

        return save(true,baseUniv,request
					,logoFile
					,backGroundFile
		);
    }

    @PostMapping("/update.json")
    @Function("base.baseUniv.edit")
    @ResponseBody
    public JsonResult<String> update(@RequestBody(required = false) BaseUniv baseUniv,HttpServletRequest request) {
	    JsonResult result = JsonResult.success();
        CoreUser user = getLoginUser(request);
        if(baseUniv.getIs211()!=null){
            baseUnivService.updateBaseUnivById(baseUniv.getUnivId(),"is_211",baseUniv.getIs211());
        }else if(baseUniv.getIs985()!=null){
            baseUnivService.updateBaseUnivById(baseUniv.getUnivId(),"is_985",baseUniv.getIs985());
        }else{
            baseUnivService.updateTemplate(baseUniv);
        }
        return result;
    }


   
    @GetMapping("/view.json")
    @Function("base.baseUniv.query")
    @ResponseBody
    public JsonResult<BaseUniv>queryInfo(Integer univId) {
        BaseUniv baseUniv = baseUnivService.queryById( univId);
        return  JsonResult.success(baseUniv);
    }

    @PostMapping("/delete.json")
    @Function("base.baseUniv..del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        baseUnivService.batchDelBaseUniv(idList);
        return JsonResult.success();
    }

	@PostMapping("/getCityByProvId.json")
	@Function("base.baseUniv.query")
	@ResponseBody
	public List<BaseEntity> getCityByProvId(Integer id) {
		return baseUnivService.selectCityByProvId(id);
	}


	@GetMapping("/getProvinceList.json")
	@Function("base.baseUniv.query")
	@ResponseBody
	public List<BaseEntity> getProvinceList() {
		return baseUnivService.selectProvince();
	}

}
